package com.king.netty.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * 自定义管道初始化
 * @author unidentifiable
 * @date 2021-03-06
 */
public class TestServerInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * 初始化管道
     * @param socketChannel 管道
     * @throws Exception
     */
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        // 得到管道
        ChannelPipeline pipeline = socketChannel.pipeline();

        // 加入一个 netty 提供的 HttpServerCodec codec => decoder，该类是 netty 提供的 http 编-解码器
        pipeline.addLast("MyHttpServerCodec", new HttpServerCodec());

        // 加入自定义服务器处理器
        pipeline.addLast("MyTestHttpServerHandler", new TestHttpServerHandler());
    }
}
